var util = require('../../utils/util.js')
var api = require('../../config/api.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    content: "",
    img: '/style/images/jia.svg',
    setInter: "",
    btnSubmitFlag:false,
    coachs:[],
    coachId:'',
    checked: false,
    offsetTop:''
  },
  //是否选中
checkedTap: function (e) {
  // console.log(JSON.stringify(e))
  if(this.data.offsetTop == e.target.offsetTop){
    this.setData({
      coachId: '',
      offsetTop:'',
      "checked": false
    });
    return;
  }else{
    this.setData({
      offsetTop:e.target.offsetTop
    })
  }
  if (e.detail.value =='') {
   this.data.checked = false   //表示未选中
 }
 else {
   this.data.checked = true  //表示选中状态
 }
 },
 //投诉教练弹窗
 complaint: function (e) {
  var that = this
    if(this.data.checked){
      wx.showModal({
        title: '提示',
        content: '确定要投诉教练吗？',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            that.bindSave()
          } else if (res.cancel) {
            console.log('用户点击取消')
            that.data.checked = true
          }
        }
      })
    }else {
      that.bindSave()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryWxBindCoach();
  },
  queryWxBindCoach:function(){
    var userid = wx.getStorageSync("userid");
    if(userid == ''){
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
      return;
    }
    var that = this
    util.request(api.getWxBindCoachUrl, { uid:userid, key:api.key}, 'GET').then(function (res) {
     if(res.errno ===0 ){
         // console.log(JSON.stringify(res));
      that.setData({
        coachs:res.data
      })
      }else if(res.errno === 402){
        wx.showModal({
          title: '错误',
          content:'不被允许的请求',
          showCancel: false
        })
      }
    })


  },
  radioChange:function(e){
    // console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      coachId: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    clearInterval(that.data.setInter)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindCancel: function () {
    wx.navigateBack({})
  },
  uploadImg: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        if (!that.checkImageFormat(tempFilePaths[0])) {
          wx.showModal({
            title: '提示',
            content: '上传了不被允许的文件格式,只允许jpg格式',
            showCancel: false
          })
          return;
        }
        wx.uploadFile({
          url: api.StorageUpload,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'key':api.key
          },
          success(res) {
            var _res = JSON.parse(res.data);
            if (_res.errno === 0) {
              var url = _res.data.url
              that.setData({
                img: url
              });
            }else if(_res.errno === 402){
              wx.showModal({
                title: '错误',
                content: _res.errmsg,
                showCancel: false
              })
            }
          },
          fail(res) {
            wx.showModal({
              title: '错误',
              content: '上传失败',
              showCancel: false
            })
          }
        });
      }
    })
  },
      //照片格式校验
      checkImageFormat: function (filePath) {
        var regExp = /\.(jpg)$/;
        return regExp.test(filePath);
      },
  bindSave: function (e) {
    var that = this;
    var hasLogin = wx.getStorageSync("app.globalData.hasLogin");
    if (!hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }

    if (that.data.content == '') {
      util.showErrorToast('请填写意见...');
      return false;
    }

    var imgUrl = "";
    if (this.data.img != '/style/images/jia.svg') {
      imgUrl = this.data.img;
    }
    that.setData({
      btnSubmitFlag:true
    });
    var userid = wx.getStorageSync("userid");
    if(userid == ''){
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
      return;
    }

    
    util.request(api.SuggestionSave, {
      content: that.data.content,
      img: imgUrl,
      uid:userid,
      coachId:that.data.coachId,
      key:api.key
    }, 'POST').then(function (res) {
      wx.showLoading({
        title: '提交中...',
        mask: true
      });
      if (res.errno === 0) {
        wx.hideLoading();
          wx.showToast({
            title: '感谢您的反馈！',
            icon: 'success',
            duration: 2000,
            complete: function () {
              that.data.setInter = setInterval(function () {
                wx.switchTab({
                  url: '/pages/user/user',
                })
              }, 2000);

              /*that.setData({
                img: '/style/images/jia.svg',
                content:''
              });*/
            }
          });
      }else if(res.errno === 402){
        wx.showModal({
          title: '错误',
          content:'不被允许的请求',
          showCancel: false
        })
    }else {
        wx.hideLoading();
        util.showErrorToast(res.errmsg);
      }
    });
  },
  contentInput: function (e) {
    this.setData({
      content: e.detail.value,
    });
  }

})